<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Title_FormItem" prop="Entity.Title" :rules="[{ required: true, message: '标题为必填项', trigger: 'blur' }]">
								<template #label>
									<WtmLabelTooltip label="标题" tooltip="通知消息的标题" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Title" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Content_FormItem" prop="Entity.Content">
								<template #label>
									<WtmLabelTooltip label="内容" tooltip="通知消息的详细内容" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Content" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_RouteId_FormItem" prop="Entity.RouteId">
								<template #label>
									<WtmLabelTooltip label="路由" tooltip="处理该待办的页面路由" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.RouteId" disabled class="width100">
									<el-option v-for="item in stateData.AllSysBillRoutes" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<el-row :gutter="20">
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_CategoryId_FormItem" prop="Entity.CategoryId" label="消息分类">
							<el-select v-model="stateData.vmModel.Entity.CategoryId" disabled class="width100">
								<el-option v-for="item in stateData.AllSysNotificationCategorys" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
							></el-select>
						</el-form-item>
					</el-col>
				</el-row>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_BusinessType_FormItem" prop="Entity.BusinessType">
								<template #label>
									<WtmLabelTooltip label="业务类型" tooltip="通知消息的业务类型" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.BusinessType" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_BusinessId_FormItem" prop="Entity.BusinessId">
								<template #label>
									<WtmLabelTooltip label="业务数据ID" tooltip="关联的业务数据ID" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.BusinessId" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>人员信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SenderName_FormItem" prop="Entity.SenderName">
								<template #label>
									<WtmLabelTooltip label="发送人姓名" tooltip="发送人的姓名" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SenderName" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ReceiverName_FormItem" prop="Entity.ReceiverName">
								<template #label>
									<WtmLabelTooltip label="接收人姓名" tooltip="接收人的姓名" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ReceiverName" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>状态信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_Status_FormItem"
								prop="Entity.Status"
								:rules="[{ required: true, message: '阅读状态为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="阅读状态" tooltip="通知消息的阅读状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.Status" disabled class="width100">
									<el-option key="未读" value="未读" label="未读"></el-option>
									<el-option key="已读" value="已读" label="已读"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_Priority_FormItem"
								prop="Entity.Priority"
								:rules="[{ required: true, message: '优先级为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="优先级" tooltip="通知消息的优先级" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.Priority" disabled class="width100">
									<el-option key="低" value="低" label="低"></el-option>
									<el-option key="普通" value="普通" label="普通"></el-option>
									<el-option key="高" value="高" label="高"></el-option>
									<el-option key="紧急" value="紧急" label="紧急"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_IsTop_FormItem"
								prop="Entity.IsTop"
								:rules="[{ required: true, message: '是否置顶为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="是否置顶" tooltip="是否置顶显示" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.IsTop" disabled class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>时间信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SendTime_FormItem" prop="Entity.SendTime">
								<template #label>
									<WtmLabelTooltip label="发送时间" tooltip="通知消息的发送时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.SendTime" disabled class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ReadTime_FormItem" prop="Entity.ReadTime">
								<template #label>
									<WtmLabelTooltip label="阅读时间" tooltip="通知消息的阅读时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.ReadTime" disabled class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysNotificationApi as api } from '/@/views/_admin/sysnotification/api';
import other from '/@/utils/other';
import { ISysNotificationVm } from '/@/views/_admin/sysnotification/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysNotificationVm>>({
	vmModel: {
		Entity: {
			ID: null,
			Title: null,
			Content: null,
			RouteId: null,
			CategoryId: null,
			BusinessType: null,
			BusinessId: null,
			SenderName: null,
			ReceiverName: null,
			Status: '未读',
			Priority: '低',
			IsTop: false,
			SendTime: new Date(),
			ReadTime: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
	AllSysBillRoutes: [] as ISysSelectType[],
	AllSysNotificationCategorys: [] as ISysSelectType[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		other.getSelectList('/api/SysNotification/GetSysBillRoutes', [], false).then((x) => (stateData.AllSysBillRoutes = x)),
		other.getSelectList('/api/SysNotification/GetSysNotificationCategorys', [], false).then((x) => (stateData.AllSysNotificationCategorys = x)),
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
